package linear

// SequenceList 切片实现顺序表
type SequenceList struct {
    len int
    arr []interface{}
}

func NewSequenceList(cap int) *SequenceList {
    return &SequenceList{
        len: 0,
        arr: make([]interface{}, 0, cap),
    }
}

func (sl *SequenceList) Clear() {
    sl.len = 0
}

func (sl *SequenceList) IsEmpty() bool {
    return sl.len == 0
}

func (sl *SequenceList) Length() int {
    return sl.len
}

func (sl *SequenceList) Get(index int) interface{} {
    return sl.arr[index]
}

func (sl *SequenceList) Insert(value ...interface{}) {
    sl.len += len(value)
    sl.arr = append(sl.arr, value...)
}

func (sl *SequenceList) InsertI(index int, value interface{}) {
    sl.arr = append(sl.arr, 0)
    sl.len++
    for i := sl.len - 1; i > index; i-- {
        sl.arr[i] = sl.arr[i-1]
    }
    sl.arr[index] = value
}

func (sl *SequenceList) Remove(index int) interface{} {
    indexValue := sl.arr[index]
    for i := index; i < sl.len-1; i++ {
        sl.arr[i] = sl.arr[i+1]
    }
    sl.len--
    sl.arr = sl.arr[0:sl.len]
    return indexValue
}

// IndexOf 因为是空接口，所以不能直接比较
func (sl *SequenceList) IndexOf(value interface{}) int {
    for i := 0; i < sl.len-1; i++ {
        if sl.arr[i] == value {
            return i
        }
    }
    return -1
}
